package org.eae.eldf.service.module.impl;


import java.util.Date;
import java.util.List;
import org.eae.eldf.dao.module.IModuleCompanyDao;
import org.eae.eldf.dao.module.impl.ModuleCompanyDaoImpl;
import org.eae.eldf.entity.module.ModuleCompany;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.service.BaseServiceImpl;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.module.ModuleCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class ModuleCompanyServiceImpl  extends BaseServiceImpl<ModuleCompany> implements ModuleCompanyService{
	
	private IModuleCompanyDao moduleCompanyDao;
	
	
	
	@Autowired
	public void setModuleCompanyDao(ModuleCompanyDaoImpl moduleCompanyDao) {
		this.moduleCompanyDao = moduleCompanyDao;
		this.dao = moduleCompanyDao;
	}
	
	/**
	 * 查询ModuleCompany
	 */
	@Override
	public Pagination<ModuleCompany> queryListModuleCompany(
			Pagination<ModuleCompany> pager, String searchValue) {
		 pager = moduleCompanyDao.queryListModuleCompany(pager, searchValue);
		 return pager;
	}
	/**
	 * 删除ModuleCompany
	 */
	@Override
	public void deleteModuleCompany(Long id) {
		moduleCompanyDao.delete(Criteria.create(ModuleCompany.class).where("moduleCompanyId", new Object[]{id}));	
	}
	/**
	 * 通过companyCode查询moduleCompany
	 */
	@Override
	public List<ModuleCompany> findByModuleCompanyByCompanyCode(String id) {
		return moduleCompanyDao.findByModuleCompanyByCompanyCode(id);
	}
	/**
	 * company增加module(增加之前删除company的所有的module)
	 */
	@Override
	public void insertModuleCompany(String moduleInfoIds,String companyCode ) {
		
		moduleCompanyDao.delete(Criteria.create(ModuleCompany.class).where("companyCode", new Object[]{companyCode}));
		String[] ids = moduleInfoIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				ModuleCompany moduleCompany = new ModuleCompany();
				moduleCompany.setModuleInfoId(Long.valueOf(ids[i]));
				moduleCompany.setCompanyCode(companyCode);
				moduleCompany.setCreateTime(new Date());
				moduleCompany.setCreateUser(SpringSecurityUtils.getCurrentUserName());
				moduleCompany.setLastUpdateTime(new Date());
				moduleCompany.setLastUpdateUser(SpringSecurityUtils.getCurrentUserName());
				dao.insert(moduleCompany);
			}
			
	}

	@Override
	public void saveCompanyModule(String companyCode, List<Long> mids) {
		moduleCompanyDao.delete(Criteria.create(ModuleCompany.class).where("companyCode", new Object[]{companyCode}));
		if(null!=mids){
			for(Long pid:mids){
				ModuleCompany moduleCompany = new ModuleCompany();
				moduleCompany.setModuleInfoId(pid);
				moduleCompany.setCompanyCode(companyCode);
				moduleCompany.setCreateTime(new Date());
				moduleCompany.setCreateUser(SpringSecurityUtils.getCurrentUserName());
				moduleCompany.setLastUpdateTime(new Date());
				moduleCompany.setLastUpdateUser(SpringSecurityUtils.getCurrentUserName());
				dao.insert(moduleCompany);
			}
		}
		
	}




}
